<?php
/*
 * IKPHP 爱客开源社区 @copyright (c) 2012-3000 IKPHP All Rights Reserved 
 * @author 小麦
 * @Email:ikphp@sina.cn
 * @公共推荐控制器 2014年9月3日11:31:47
 */
namespace IKPHP\Apps\Home\Controllers;

use IKPHP\Common\Controllers\FrontendController;
use IKPHP\Common\Models\Recommend;
use IKPHP\Common\Models\LikeModel;
use IKPHP\Common\Models\VirtualModel;

class SnsController extends FrontendController
{
	private $userid = 0;
	
    public function initialize()
    {
        parent::initialize();
        // 访问者控制
		if ($this->visitor){
			$this->userid = $this->visitor['userid'];
		}  
		$this->vimodel = new VirtualModel();    
    }
	// 推荐
	public function recommendAction(){
		//访问控制
		if(!$this->visitor){return $this->_redirect('home/user/login');}
		
		if(IS_POST && IS_AJAX){
			
			$ck				= $this->_post('ck');
			$content		= $this->_post('content');		
			$typeid		    = $this->_post('tid');
			$type		    = $this->_post('tkind');
			$uid		    = $this->_post('uid'); //当前推荐人id也就是登陆者
			
			$userid = $this->userid;

			if ($ck == $this->visitor['site_ck']){
				
	        	$jsonData = array();	
				if(empty($typeid) || empty($type) || empty($uid)){
					$jsonData = array('r'=>1,'html'=>'你可以去火星了！');
					return $this->ajaxReturn($jsonData);
				}

				if($userid != $uid){
					$jsonData = array('r'=>1,'html'=>'非法用户！');
					return $this->ajaxReturn($jsonData);
				}
								
		        //先查寻判断数据是否存在于该模型如：日志模型user_note
		        $type = base64_decode($type);
				$this->vimodel->setviSource($type);
				$pkey = $this->vimodel->getPk();
		
				$strMod = $this->vimodel->findFirst("$pkey = $typeid");
				if(!$strMod){
					$jsonData = array('r'=>1,'html'=>'没有该虚拟模型数据');
					return $this->ajaxReturn($jsonData);
				}
				// 判断是否已经推荐
				if(Recommend::isRecommend($uid, $typeid, $type)){
					$jsonData = array('r'=>1,'html'=>'你已经推荐过了；不要重复推荐哦');
					return $this->ajaxReturn($jsonData);				
				}
				// 组织数据
				$arrData = array (
						'typeid'	=> $typeid,
						'type'		=> $type,
						'userid'	=> $this->userid,
						'content'	=> $content,
						'addtime'	=> time(),
				);
				//添加
				$newRecd = new Recommend();
				if (false !== $newRecd->create( $arrData )) {
					$jsonData = array('r'=>0,'num'=>$newRecd->count_recommend);
					// TODO: 记录日志 更新评论数
					return $this->ajaxReturn($jsonData);
				}else{
					$jsonData = array('r'=>1,'html'=>'推荐失败了');
					return $this->ajaxReturn($jsonData);					
				}
				
	        }else{
	        	$jsonData = array('r'=>1,'html'=>'Token会话失败！');
	        	return $this->ajaxReturn($jsonData);
	        }
		}else{
			return $this->error('你访问的页面不存在！');
		}
	}
		
	// 喜欢
	public function likeAction(){

		//访问控制
		if(!$this->visitor){return $this->_redirect('home/user/login');}
		
		if(IS_POST && IS_AJAX){
			
			$ck				= $this->_post('ck');
			$content		= $this->_post('content','trim','0');		
			$typeid		    = $this->_post('tid');
			$type		    = $this->_post('tkind');
			$uid		    = $this->_post('uid'); //当前推荐人id也就是登陆者
			
			$userid = $this->userid;

			if ($ck == $this->visitor['site_ck']){
				
	        	$jsonData = array();	
				if(empty($typeid) || empty($type) || empty($uid)){
					$jsonData = array('r'=>1,'html'=>'你可以去火星了！');
					return $this->ajaxReturn($jsonData);
				}

				if($userid != $uid){
					$jsonData = array('r'=>1,'html'=>'非法用户！');
					return $this->ajaxReturn($jsonData);
				}
								
		        //先查寻判断数据是否存在于该模型如：日志模型user_note
		        $type = base64_decode($type);
				$this->vimodel->setviSource($type);
				$pkey = $this->vimodel->getPk();
		
				$strMod = $this->vimodel->findFirst("$pkey = $typeid");
				if(!$strMod){
					$jsonData = array('r'=>1,'html'=>'没有该虚拟模型数据');
					return $this->ajaxReturn($jsonData);
				}
				// 判断是否已经推荐
				if(LikeModel::isLiked($uid, $typeid, $type)){
					$jsonData = array('r'=>1,'html'=>'你已经喜欢过了；不要重复推荐哦');
					return $this->ajaxReturn($jsonData);				
				}
				// 组织数据
				$arrData = array (
						'typeid'	=> $typeid,
						'type'		=> $type,
						'userid'	=> $this->userid,
						'content'	=> $content,
						'addtime'	=> time(),
				);
				//添加
				$newRecd = new LikeModel();
				if (false !== $newRecd->create( $arrData )) {
					$jsonData = array('r'=>0,'num'=>$newRecd->count_like);
					// TODO: 记录日志 更新评论数
					return $this->ajaxReturn($jsonData);
				}else{
					$jsonData = array('r'=>1,'html'=>'喜欢失败了');
					return $this->ajaxReturn($jsonData);					
				}
				
	        }else{
	        	$jsonData = array('r'=>1,'html'=>'Token会话失败！');
	        	return $this->ajaxReturn($jsonData);
	        }
		}else{
			return $this->error('你访问的页面不存在！');
		}
				
	}   
}